package mes.kanban.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesDailyChemicalFirstItem;
import mes.kanban.respository.DailyChemicalFirstItemRespository;
import mes.kanban.service.DailyChemicalFirstServiceI;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.vo.EchartsDataVO;
import mes.kanban.vo.MesWorkshopVO;

@Service
public class DailyChemicalFirstServiceImpl implements DailyChemicalFirstServiceI {
	
	private static final Logger log = LoggerFactory.getLogger(DailyChemicalFirstServiceImpl.class);
	
	@Autowired
	private DailyChemicalFirstItemRespository dailyChemicalFirstItemRespository;
	
	@Override
	public List<MesWorkshopVO> getDatas() {
		log.info("====Table====");
		List<MesWorkshopVO> reportVos = new ArrayList<MesWorkshopVO>();
		List<MesDailyChemicalFirstItem> reports = dailyChemicalFirstItemRespository.findAll();
		MesWorkshopVO vo = null;
		if(!reports.isEmpty()) {
			for(MesDailyChemicalFirstItem report : reports) {
				vo = new MesWorkshopVO();
				BeanUtils.copyProperties(report,vo);
				reportVos.add(vo);
			}
		}
		return reportVos;
	}

	@Override
	public EchartsDataVO queryOutputData() {
		log.info("====折线图====");
		EchartsDataVO vo = new EchartsDataVO();
		List<MesDailyChemicalFirstItem> reports = dailyChemicalFirstItemRespository.findAll();
		List<String> prodNameList = new  ArrayList<String>();
		List<BigDecimal> plannedOutputList = new  ArrayList<BigDecimal>();
		List<BigDecimal> currentOutputList = new  ArrayList<BigDecimal>();
		
		if(!reports.isEmpty()) {
			for(MesDailyChemicalFirstItem report : reports) {
				prodNameList.add(report.getInProductionName());
				plannedOutputList.add(report.getPlannedOutput());
				currentOutputList.add(report.getCurrentOutput());
			}
		}
		vo.setProdNameList(prodNameList);
		vo.setPlannedOutputList(plannedOutputList);
		vo.setCurrentOutputList(currentOutputList);
		
		return vo;
	}

	@Override
	public EchartsDataVO queryTimeScheduleData() {
		log.info("====折线图====");
		EchartsDataVO vo = new EchartsDataVO();
		List<MesDailyChemicalFirstItem> reports = dailyChemicalFirstItemRespository.findAll();
		List<String> prodNameList = new  ArrayList<String>();
		List<Integer> timeScheduleList = new  ArrayList<Integer>();;
		
		if(!reports.isEmpty()) {
			for(MesDailyChemicalFirstItem report : reports) {
				prodNameList.add(report.getInProductionName());
				String value = report.getTimeSchedule();
				if (value.contains("%")) {
					value = value.split("%")[0];
				}
				timeScheduleList.add(Integer.valueOf(value));
			}
		}
		vo.setProdNameList(prodNameList);
		vo.setTimeScheduleList(timeScheduleList);
        
		return vo;
	}

	@Override
	public EchartsDataVO queryWeekScheduleData() {
		log.info("====柱状图====");
		EchartsDataVO vo = new EchartsDataVO();
		List<MesDailyChemicalFirstItem> reports = dailyChemicalFirstItemRespository.findAll();
		List<String> prodNameList = new  ArrayList<String>();
		List<Integer> weekProductionScheduleList = new  ArrayList<Integer>();
		List<Integer> weekPlanedScheduleList = new  ArrayList<Integer>();
		
		if(!reports.isEmpty()) {
			for(MesDailyChemicalFirstItem report : reports) {
				if(IsNotNullValit.StringIsNotNull(report.getWeekProductionSchedule())) {
					prodNameList.add(report.getInProductionName());
					String value = report.getWeekProductionSchedule();
					if (value.contains("%")) {
						value = value.split("%")[0];
					}
					weekPlanedScheduleList.add(100-Integer.valueOf(value));
					weekProductionScheduleList.add(Integer.valueOf(value));
				}
			}
		}
		vo.setProdNameList(prodNameList);
		vo.setWeekProductionScheduleList(weekProductionScheduleList);
		vo.setWeekPlanedScheduleList(weekPlanedScheduleList);
		return vo;
		
	}


}
